package com.mimiwang.chat.service.Imp;

import com.mimiwang.chat.config.TalkRedisConfiguration;
import com.mimiwang.chat.entity.Talk;
import com.mimiwang.chat.entity.TalkListVo;
import com.mimiwang.chat.entity.TalkNoticeVo;
import com.mimiwang.chat.entity.TalkVo;
import com.mimiwang.chat.service.ChatTalkRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * @AUTHOR:游李滨
 * @DATE:2022/10/11 19:59
 */

@Service
public class ChatTalkRedisServiceImp implements ChatTalkRedisService {



    @Autowired
    private TalkRedisConfiguration talkRedisConfiguration;


    @Autowired
    RedisTemplate<String,Object> redisTemplate;




    @Override
    public String getRedisStoreTalkKey(long senderId,long receiverId) {
       return talkRedisConfiguration.getRedisPrefix()+":"+senderId+":"+receiverId;
    }

    @Override
    public String getRedisReceiverTalkKey(long receiverId) {
        return  talkRedisConfiguration.getRedisPrefix()+":*:"+receiverId;
    }

    @Override
    public String getRedisSenderTalkKey(int senderId) {
        return talkRedisConfiguration.getRedisPrefix()+":"+senderId+":*";
    }

    @Override
    public int insertTalkToRedis(Talk talk) {

        String redisStoreTalkKey = this.getRedisStoreTalkKey(talk.getSender(), talk.getReceiver());

        redisTemplate.opsForValue().increment(redisStoreTalkKey);

        return 1;
    }

    @Override
    public List<TalkNoticeVo> selectReceiverTalk(int receiverId) {

        String redisReceiverTalkKey = this.getRedisReceiverTalkKey(receiverId);
        Set<String> keys = redisTemplate.keys(redisReceiverTalkKey);
        LinkedList<TalkNoticeVo> lst = new LinkedList<>();
        if(keys==null){
            return null;
        }
        for (String key : keys) {
            TalkNoticeVo talkNoticeVo = this.selectOneReceiverTalk(key);
            lst.add(talkNoticeVo);
        }

        return lst;
    }

    private TalkNoticeVo selectOneReceiverTalk(String key){
        Object o = redisTemplate.opsForValue().get(key);
        if(o==null){
            return null;
        }
//        获取发送者id和接收者id
        String[] split = key.split(":");
        int sender = Integer.parseInt(split[1]);
        int receiver=Integer.parseInt(split[2]);

        return new TalkNoticeVo(sender, receiver, (Integer) o);
    }
    @Override
    public List<TalkNoticeVo> selectSenderTalk(int senderId) {
        String redisSenderTalkKey = this.getRedisSenderTalkKey(senderId);
        Set<String> keys = redisTemplate.keys(redisSenderTalkKey);
        return null;
    }

    @Override
    public int deleteTalk(String key) {
        return 0;
    }

    @Override
    public int deleteTalkByReceiverId(long receiverId) {
        String redisStoreTalkKey = this.getRedisReceiverTalkKey(receiverId);
        Set<String> keys = redisTemplate.keys(redisStoreTalkKey);
        if(keys == null){
            return 0;
        }
        for (String key : keys) {
            Boolean delete = redisTemplate.delete(key);
        }
        return 1;
    }

    @Override
    public int deleteTalk(List<String> keys) {
        return 0;
    }

}
